package shopping.event;

import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import shopping.beans.CD;

public class CheckoutEventHandler extends EventHandlerBase {
	private ResourceBundle bundle = ResourceBundle.getBundle("URL"); //URL.properties
	
	@Override
	public String getURL_To_Dispatch() {
		return bundle.getString("CHECKOUT"); //return "/Checkout.jsp";
	}
	
	@Override
	public void process(HttpServletRequest request, HttpServletResponse response) {
		
		float total = 0;
		for (int i = 0; i < getCDList().size(); i++) {
		    CD anOrder = (CD) getCDList().get(i);
		    float price = anOrder.getPrice();
		    int qty = anOrder.getQuantity();
		    total += (price * qty);
		}
		
		total += 0.005;
		
		String amount = new Float(total).toString();
		int n = amount.indexOf('.');
		amount = amount.substring(0, n + 3);
		
		request.setAttribute("amount", amount);
	}
}